package View;



import Model.Action;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.ImageObserver;
import java.util.ArrayList;


public class DraggableImageComponent extends DraggableComponent implements ImageObserver {

    protected Image image;
    protected Image[] images;
    boolean rotate=false;
    public ArrayList<Action> listOfactions;
    private boolean autoSize = false;
    private Dimension autoSizeDimension = new Dimension(0, 0);
    private MainMenu refMenu;
    public String selectedCharacterType = "";

    public DraggableImageComponent(int id, MainMenu refmenu) {
        super( id, refmenu);
        this.refMenu = refmenu;
        listOfactions = new ArrayList<Action>();
        addDragListeners();
        setLayout(null);
        setBackground(Color.black);
    }
    
    
    public DraggableImageComponent(int id, MainMenu refmenu, String characterType) {
        super( id, refmenu);
        this.refMenu = refmenu;
        this.selectedCharacterType = characterType;
        listOfactions = new ArrayList<Action>();
        addDragListeners();
        setLayout(null);
        setBackground(Color.black);
    }


    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        if (image != null) {
            setAutoSizeDimension();
            g2d.drawImage(image, 0, 0, getWidth(), getHeight(), this);
        } else {
            g2d.setColor(getBackground());
            g2d.fillRect(0, 0, getWidth(), getHeight());
        }
    }
    
    public void addAction(int idDrag, int velocity, String type, String sound){
        Action newAction = new Action(idDrag,velocity,type, sound,image);
        this.listOfactions.add(newAction);
    }
    
    public void addAction(int idDrag, int velocity, String type, String sound,String characterType){
        Action newAction = new Action(idDrag,velocity,type, sound,image,characterType);
        this.listOfactions.add(newAction);
    }
    
    public void updateAction(int idAccion, int velocity, String type,String sound){
        for (int i = 0; i < listOfactions.size(); i++) {
            Action current = this.listOfactions.get(i);
            if(current.getID()== idAccion){
               current.setDx(velocity);
               current.setDy(velocity);
               current.setMovementType(type);
               current.setSoundPath(sound);
               current.setactorType(image);
               this.listOfactions.set(i, current);
            }
        }
    }
    
    
    public void deleteAccion(int idDelete){
        
        for (int i = 0; i < listOfactions.size(); i++) {
            Action current = this.listOfactions.get(i);
            if(current.getID()== idDelete){
                this.listOfactions.remove(i);
            }
        }
    }


    private Dimension adaptDimension(Dimension source, Dimension dest) {
        int sW = source.width;
        int sH = source.height;
        int dW = dest.width;
        int dH = dest.height;
        double ratio = ((double) sW) / ((double) sH);
        if (sW >= sH) {
            sW = dW;
            sH = (int) (sW / ratio);
        } else {
            sH = dH;
            sW = (int) (sH * ratio);
        }
        return new Dimension(sW, sH);
    }
    



    @Override
    public boolean imageUpdate(Image img, int infoflags, int x, int y, int w, int h) {
        if (infoflags == ALLBITS) {
            repaint();
            setAutoSizeDimension();
            return false;
        }
        return true;
    }
    
    @Override
    public  int getid(){
        return this.id;
    }

    private void setAutoSizeDimension() {
        if (!autoSize) {
            return;
        }
        if (image != null) {
            if (image.getHeight(null) == 0 || getHeight() == 0) {
                return;
            }
            if ((getWidth() / getHeight()) == (image.getWidth(null) / (image.getHeight(null)))) {
                return;
            }
            autoSizeDimension = adaptDimension(new Dimension(image.getWidth(null), image.getHeight(null)), this.getSize());
            setSize(autoSizeDimension.width, autoSizeDimension.height);
        }
    }

    public void grow(int pixels) {
        double ratio = getWidth() / getHeight();
        setSize(getSize().width + pixels, (int) (getSize().height + (pixels / ratio)));
    }

    public boolean isAutoSize() {
        return autoSize;
    }

    public void setAutoSize(boolean autoSize) {
        this.autoSize = autoSize;
    }

    public Image getImage() {
        return image;
    }
    
    public Image getImageatIndex(int index) {
        return images[index];
    }

    public void setImage(String image) {
        setImage(Toolkit.getDefaultToolkit().getImage(image));
    }

    public void setImage(Image image) {
        this.image = image;
        repaint();
        setAutoSizeDimension();
    }
    
    private void addDragListeners() {
        final DraggableImageComponent handle = this;
        addMouseListener(new MouseListener() {

            @Override
            public void mouseClicked(MouseEvent e) {
                //throw new UnsupportedOperationException("Not supported yet.");
                CharacterActionForm actionForm = new CharacterActionForm(handle, refMenu);
                actionForm.setVisible(true);
            }

            @Override
            public void mousePressed(MouseEvent e) {
                //throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                //throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                //throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public void mouseExited(MouseEvent e) {
                //throw new UnsupportedOperationException("Not supported yet.");
            }
     });
   }
}
